//
//  ZBCollectionMaker.m
//  XZBProduct
//
//  Created by xzb on 2018/7/23.
//  Copyright © 2018年 xzb. All rights reserved.
//

#import "ZBCollectionMaker.h"


@implementation ZBCollectionViewSectionMaker

- (instancetype)init
{
    self = [super init];
    if (self) {
        _section = [[ZBSection alloc] init];
    }
    return self;
}

@end


@implementation ZBCollectionRowMaker

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.row = [[ZBRow alloc] init];
    }
    
    return self;
}

- (ZBCollectionRowMaker *(^)(CGSize))itemSize
{
    return ^id(CGSize size){
        self.row.itemSize = size;
        return self;
    };
}

- (ZBCollectionRowMaker *(^)(CGFloat))height
{
    return ^id(CGFloat h){
        self.row.height = h;
        return self;
    };
}

- (ZBCollectionRowMaker *(^)(id))model
{
    return ^id(id m){
        self.row.rawModel = m;
        return self;
    };
}

- (ZBCollectionRowMaker *(^)(NSString *))tag
{
    return ^id(NSString *t){
        self.row.tag = t;
        return self;
    };
}

- (ZBCollectionRowMaker *(^)(SEL))configureSEL
{
    return ^id(SEL s){
        self.row.configSel = s;
        return self;
    };
}

- (ZBCollectionRowMaker *(^)(SEL))itemSizeSEL
{
    return ^id(SEL s){
        self.row.itemSizeSel = s;
        return self;
    };
}

- (ZBCollectionRowMaker *(^)(SEL))heightSEL
{
    return ^id(SEL s){
        self.row.heightSel = s;
        return self;
    };
}

- (ZBCollectionRowMaker *(^)(UICollectionViewCell * (^)(id)))constructBlock
{
    return ^id( UICollectionViewCell * (^block)(id) ){
        self.row.constructBlock = block;
        return self;
    };
}

- (ZBCollectionRowMaker *(^)(void (^)(id)))selectBlock
{
    return ^id( void (^selectHandler)(id) ){
        self.row.selectHandler = selectHandler;
        return self;
    };
}

- (ZBCollectionRowMaker *(^)(NSDictionary *))extraDictBlock {
    return ^ZBCollectionRowMaker *(NSDictionary *extraDict) {
        self.row.extraDict = [extraDict copy];
        return self;
    };
}

- (ZBCollectionRowMaker *(^)(NSBundle *))bundle {
    return ^ZBCollectionRowMaker *(NSBundle *bundle) {
        self.row.bundle = bundle;
        return self;
    };
}

@end

@interface ZBCollectionMaker()

@property (nonatomic, strong) NSMutableArray *sections;

@property (nonatomic, strong) ZBCollectionViewSectionMaker *currentSectionMaker;

@end

@implementation ZBCollectionMaker

- (instancetype)init
{
    self = [super init];
    if (self) {
        _sections = [NSMutableArray array];
    }
    return self;
}

- (ZBCollectionViewSectionMaker *(^)(NSString *key))section
{
    return ^id(NSString *key){
        ZBCollectionViewSectionMaker *sectionMaker = [[ZBCollectionViewSectionMaker alloc] init];
        [self->_sections addObject:sectionMaker];
        self->_currentSectionMaker = sectionMaker;
        sectionMaker.section.key = key;
        return sectionMaker;
    };
}

- (ZBCollectionRowMaker *(^)(NSString *))sectionHeader
{
    return ^id(NSString *proto){
        ZBCollectionRowMaker *rowMaker = [[ZBCollectionRowMaker alloc] init];
        rowMaker.row.protoType = proto;
        self->_currentSectionMaker.section.headerView = rowMaker.row;
        return rowMaker;
    };
}

- (ZBCollectionRowMaker *(^)(NSString *))sectionFooter
{
    return ^id(NSString *proto){
        ZBCollectionRowMaker *rowMaker = [[ZBCollectionRowMaker alloc] init];
        rowMaker.row.protoType = proto;
        self->_currentSectionMaker.section.footerView = rowMaker.row;
        return rowMaker;
    };
}

- (ZBCollectionRowMaker *(^)(NSString *))row
{
    return ^id(NSString *proto){
        ZBCollectionRowMaker *rowMaker = [[ZBCollectionRowMaker alloc] init];
        rowMaker.row.protoType = proto;
        [self->_currentSectionMaker.section appendRows:@[rowMaker.row]];
        return rowMaker;
    };
}

- (NSArray *)install
{
    return [_sections bk_map:^id(ZBCollectionViewSectionMaker *sectionMaker) {
        return sectionMaker.section;
    }];
}

@end
